home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snpd0492.zip / STUB.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  3KB  |  115 lines

  1. /*
  2. **  STUB.C - Utility to truncate files
  3. **
  4. **      STUB is used with MAKE utilities which lack the ability to timestamp
  5. **      library object modules to reduce disk space requirements. After
  6. **      compiling and building your library or executable, run "STUB *.OBJ"
  7. **      to truncate all object files to zero length. The files' time and
  8. **      date stamps will remain unchanged. By doing this, your make utility
  9. **      will still know which modules are out of date even though the files
  10. **      themselves have all been truncated to zero length. STUB also supports
  11. **      the standard response file format so you can use your linker response
  12. **      files to direct the files to be truncated.
  13. **
  14. **  public domain by Bob Stout
  15. **
  16. **  Notes: To expand command line arguments with wildcards,
  17. **         TC/TC++/BC++  - Link in WILDARGS.OBJ.
  18. **         MSC/QC        - Link in SETARGV.OBJ.
  19. **         ZTC/C++       - Link in _MAINx.OBJ, where 'x' is the memory model.
  20. **
  21. **         Allows file list(s) using standard "@file_list_name" convention.
  22. */
  23.  
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <dos.h>
  27. #include <io.h>
  28. #include <fcntl.h>
  29.  
  30. #define LAST_CHAR(s) (s)[strlen(s)-1]
  31.  
  32. int fd;
  33.  
  34. void truncate(char *);
  35.  
  36. int main(int argc, char **argv)
  37. {
  38.       int i;
  39.  
  40.       if (2 > argc)
  41.       {
  42.             puts("Usage: STUB filespec [...filespec]");
  43.             puts("where: filespec = fully-specified file name, or");
  44.             puts("       filespec = wildcard-specified file name, or");
  45.             puts("       filespec = response file name, e.g. \"@FILE.LST\"");
  46.             return 1;
  47.       }
  48.  
  49.       for (i = 1; i < argc; ++i)    /* Scan for simple file specs       */
  50.       {
  51.             if ('@' == *argv[i])
  52.                   continue;
  53.             else  truncate(argv[i]);
  54.       }
  55.       for (i = 1; i < argc; ++i)    /* Scan for response file specs     */
  56.       {
  57.             if ('@' == *argv[i])
  58.             {
  59.                   FILE *fp;
  60.                   char buf[256], *ptr = &argv[i][1];
  61.  
  62.                   if (NULL == (fp = fopen(ptr, "r")))
  63.                   {
  64.                         printf("\aSTUB: Error opening %s\n", ptr);
  65.                         return -1;
  66.                   }
  67.                   while (NULL != fgets(buf, 255, fp))
  68.                   {
  69.                         LAST_CHAR(buf) = '\0';  /* Strip '\n'           */
  70.                         truncate(buf);
  71.                   }
  72.                   fclose(fp);
  73.             }
  74.       }
  75.       return 0;
  76. }
  77.  
  78. /*
  79. **  The actual truncation function
  80. */
  81.  
  82. #ifdef __ZTC__
  83.  #define GETFTIME dos_getftime
  84.  #define SETFTIME dos_setftime
  85. #else
  86.  #define GETFTIME _dos_getftime
  87.  #define SETFTIME _dos_setftime
  88. #endif
  89.  
  90. void truncate(char *fname)
  91. {
  92. #ifdef __TURBOC__
  93.             struct ftime Ftime;
  94. #else
  95.             unsigned date, time;
  96. #endif
  97.  
  98.             fd = open(fname, O_WRONLY);
  99.  
  100. #ifdef __TURBOC__                         /* Save the time/date         */
  101.             getftime(fd, &Ftime);
  102. #else
  103.             GETFTIME(fd, &date, &time);
  104. #endif
  105.  
  106.             chsize(fd, 0L);               /* Truncate the file          */
  107.  
  108. #ifdef __TURBOC__                         /* Restore the time/date      */
  109.             setftime(fd, &Ftime);
  110. #else
  111.             SETFTIME(fd, date, time);
  112. #endif
  113.             close(fd);
  114. }
  115.